<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script src="/webjars/axios/0.19.0/dist/axios.js" type="text/javascript"></script>
</head>
<body>
<div id="vuebox">
  <div class="container" style="margin-top: 50px">
      <div class="row">
          <div class="col-md-6">
              <form class="form-inline" role="form" style="padding-left: 250px;">
                  <div class="form-group" style="padding-left: 20px;">
                      <label>图书分类:</label>
                      <select class="form-control" v-model="categoryid">
                          <option value="0">全部</option>
                          <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                      </select>
                  </div>
                  <div class="form-group"  style="padding-left: 20px;">
                      <button type="button" class="btn btn-primary" @click="selectEntry(1)">查询</button>
                  </div>

              </form>
          </div>
          <div class="col-md-4 text-right">
              <a href="add_entry.html" @click.prevent="addBook" class="btn btn-primary">新增电子图书</a>
          </div>
      </div>
      <br/>
      <table class="table table-striped">
          <thead>
          <tr>
              <th>图书编号</th>
              <th>图书名称</th>
              <th>图书摘要</th>
              <th>上传人</th>
              <th>上传时间</th>
              <th>操作</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="item in entries.data.list">
              <td>{{item.id}}</td>
              <td>{{item.title}}</td>
              <td>{{item.summary}}</td>
              <td>{{item.uploaduser}}</td>
              <td>{{item.creaedate}}</td>
              <td>
                  <button class="btn btn-success" @click="to_update_entry(item.id)">修改</button>
                  <button class="btn btn-danger"@click="deleteEntry(item.id)">删除</button>
              </td>
          </tr>
          </tbody>
      </table>
      <br/>
      <div class="text-center">
      <ul class="pagination">
          <li v-show="entries.data.hasPreviousPage">
              <a href="#" @click.stop="selectEntry(entries.data.prePage)">&laquo;</a>
          </li>
          <li v-for="i in entries.data.navigatepageNums" :class="{active:entries.data.pageNum==i}">
              <a href="#" @click.stop="selectEntry(i)">{{i}}</a>
          </li>
          <li v-show="entries.data.hasNextPage">
              <a href="#" @click.stop="selectEntry(entries.data.nextPage)">&raquo;</a>
          </li>
      </ul>
      </div>
  </div>

    <!--添加的模态框-->
    <div class="modal" id="mymodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" @click="hideModal()" type="button">&times;</button>
                    <div class="modal-title">
                        <p>添加图书</p>
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <input type="hidden" v-model="new_entry.id">
                        <div class="form-group">
                            <label for="categoryId" class="col-sm-2 control-label">图书分类</label>
                            <div class="col-sm-10">
                                <select id="categoryId" class="form-control" v-model="new_entry.catrgoryid">
                                    <option value="0">全部</option>
                                    <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="title" class="col-sm-2 control-label">图书名称</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.title" id="title" type="text" class="form-control" placeholder="请输入图书名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="summary" class="col-sm-2 control-label">图书摘要</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.summary" id="summary" type="text" class="form-control" placeholder="请输入图书摘要"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="uploaduser" class="col-sm-2 control-label">上传人</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.uploaduser" id="uploaduser" type="text" class="form-control" placeholder="请输入上传人">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="createdate" class="col-sm-2 control-label">上传时间</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.creaedate" id="createdate" type="text" class="form-control" placeholder="请输入上传时间">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" @click="hideModal()">关闭</button>
                    <button class="btn btn-primary" @click="saveBook">保存</button>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
<script type="text/javascript">
        var vm=new Vue({
            el:"#vuebox",
            data:{
                categoryList:{},
                categoryid:"0",
                entries:{},
                deleKentry:{},
                pageNum:"",
                new_entry:{id:'',catrgoryid:'0',title:'',summary:'',uploaduser:'',creaedate:''},
            },
            methods:{
                selectEntry:function (cid) {
                    axios.get("/seleKentry",{params:{pageNum:cid,categoryId:this.categoryid}}).then(res=>{
                        this.entries=res.data
                    this.pageNum=cid;
                    })
                },
                deleteEntry:function(id){
                axios.get("/deleKentry",{params:{id:id}}).then(res=>{
                        this.deleKentry=res.data;
                        if(this.deleKentry.result=="SUCCESS"){
                            this.selectEntry(this.pageNum);
                        }else {
                            alert(this.deleKentry.message)
                        }
                    })
        },
                //显示模态框
                addBook:function () {
                    $("#mymodal").modal('show');
                },
                //关闭模态框
                hideModal:function () {
                    $("#mymodal").modal('hide');
                    this.clearModal();
                },
                //清空模态框
                clearModal:function () {
                    $("#categoryId").val(0);
                    $("#title").val('');
                    $("#summary").val('');
                    $("#uploaduser").val('');
                    $("#createdate").val('');
                    $("#id").val('');
                },
                //添加图书信息
                saveBook:function () {
                    axios.post("/addKentry",this.new_entry).then(res=>{
                        this.deleKentry=res.data;
                        if(this.deleKentry.result=="SUCCESS"){
                            this.selectEntry(this.pageNum);
                        }else{
                            alert(this.deleKentry.message);
                        }
                        $("#mymodal").modal("hide");
                    })
                },
                //回显数据
                to_update_entry:function (id) {
                    axios.get("/seleById",{params:{id:id}}).then(res=>{
                        this.new_entry=res.data.data;
                        $("#mymodal").modal("show");
                    })
                }

            },
            mounted(){
                axios.get("/seleCategory").then(res=>{this.categoryList=res.data})
                this.selectEntry(1);
            }
    })
</script>
</html>